package com.wkhyapp.lm.weigit.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.wkhyapp.lm.R;
import com.wkhyapp.lm.http.vo.Category;

/**
 * Created by fang on 2021/10/25.
 */

public class CategoryEditorDialog {

    private Context mContext;
    private View dialog_view;
    private Category category;
    private Dialog mDialog;
    private EditText name_et;
    private TextView clear_but,add_but,title_tv;
    public CategoryEditorDialog.callBack  mCallBack;
    private String title;
    public CategoryEditorDialog(Context context,String title, Category category) {
        this.mContext = context;
        this.category = category;
        this.title = title;
        init();

    }
    public void  init(){

        mDialog = new Dialog(mContext, R.style.custom_dialog);
        dialog_view = LayoutInflater.from(mContext).inflate(R.layout.diaglog_category_editor, null);
        title_tv = (TextView)dialog_view.findViewById(R.id.title_tv);
        name_et = (EditText)dialog_view.findViewById(R.id.name_et);
        clear_but = (TextView)dialog_view.findViewById(R.id.clear_but);
        add_but = (TextView)dialog_view.findViewById(R.id.add_but);
        title_tv.setText(title);
        if(category!=null){
            name_et.setText(category.getName());
        }

        mDialog.setContentView(dialog_view);
        Window window = mDialog.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.gravity = Gravity.CENTER; // 居中位置
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(lp);

        clear_but.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
            }
        });
        add_but.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(category==null){
                    mCallBack.add(name_et.getEditableText().toString());
                }else{
                    mCallBack.update(name_et.getEditableText().toString(),category);
                }
            }
        });

    }

    public void setCallBack(CategoryEditorDialog.callBack callBack){
        this.mCallBack = callBack;
    }
    public void close(){
        mDialog.dismiss();
    }
    public void show(){
        mDialog.show();
    }

    public  interface  callBack{
        void add(String name);

        void update(String name,Category time);
    }

}